/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.reader.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bookset.entity.BkPlace;
import com.thinkgem.jeesite.modules.reader.entity.Reader;
import com.thinkgem.jeesite.modules.reader.entity.ReaderRoler;
import com.thinkgem.jeesite.modules.reader.service.ReaderRolerService;
import com.thinkgem.jeesite.modules.reader.service.ReadersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 读者管理Controller
 * @author ThinkGem
 * @version 2013-8-29
 */
@Controller
@RequestMapping(value = "${adminPath}/book/roler")
public class ReaderRolerController extends BaseController {

	@Autowired
	private ReadersService readerService;

	@Autowired
	private ReaderRolerService readerRolerService;

	@ModelAttribute
	public ReaderRoler get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return readerRolerService.get(id);
		}else{
			return new ReaderRoler();
		}
	}


	/**
	 * 角色列表
	 * @param readerRoler
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("book:roler:view")
	@RequestMapping(value = {"list", ""})
	public String list(ReaderRoler readerRoler, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ReaderRoler> page = readerRolerService.findUser(new Page<ReaderRoler>(request, response), readerRoler);
		model.addAttribute("page", page);
		return "modules/reader/readerRolerList";
	}


	/**
	 * 角色查詢列表
	 * @param readerRoler
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("book:roler:view")
	@ResponseBody
	@RequestMapping(value = {"listData"})
	public Page<ReaderRoler> listData(ReaderRoler readerRoler, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ReaderRoler> page = readerRolerService.findUser(new Page<ReaderRoler>(request, response), readerRoler);
		return page;
	}

	/**
	 * 角色添加页面
	 * @param readerRoler
	 * @param model
	 * @return
	 */
	@RequiresPermissions("book:roler:view")
	@RequestMapping(value = "form")
	public String form(ReaderRoler readerRoler, Model model) {
			if(readerRoler.getRdRoleId()!=null || "".equals(readerRoler.getRdRoleId())){
				readerRoler.setBkPlaceList(readerRolerService.getSetRolePlace(readerRoler));
			}
		model.addAttribute("readerRoler", readerRoler);
		model.addAttribute("allPlaces", readerRolerService.getIsBorrowPlace());

		return "modules/reader/readerRolerForm";
	}



	@RequiresPermissions("book:roler:edit")
	@RequestMapping(value = "save")
	public String save(ReaderRoler readerRoler, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		List<BkPlace> bkPlaceList = Lists.newArrayList();
		List<String> bkPlaceIdList = readerRoler.getBkPlaceIdList();
		for (BkPlace r : readerRolerService.getIsBorrowPlace()){
			if (bkPlaceIdList.contains(r.getPlaceid())){
				bkPlaceList.add(r);
			}
		}
		readerRoler.setBkPlaceList(bkPlaceList);
		readerRolerService.saveRoler(readerRoler);
		addMessage(redirectAttributes, "保存用户'" + readerRoler.getRdRoleId() + "'成功");
		return "redirect:" + adminPath + "/book/roler/list?repage";
	}



	@RequiresPermissions("book:roler:edit")
	@RequestMapping(value = "delete")
	public String delete(ReaderRoler readerRoler, RedirectAttributes redirectAttributes,Reader reader, HttpServletRequest request, HttpServletResponse response) {
		String delflag = "0";//判断是否可以删除的标志位

		//1查询所有的用户列表
		Page<Reader> page = readerService.findUser(new Page<Reader>(request, response), reader);

		for(int i = 0 ;i<page.getList().size();i++){


			//1判断当前角色是否正在使用中
			if (String.valueOf(page.getList().get(i).getRdRoleId()).equals(readerRoler.getRdRoleId())){
				delflag  ="1";
				break;
			}
		}

		if ("1".equals(delflag)){
		addMessage(redirectAttributes, "删除角色失败, 不允许删除当前角色");

		}else{
			readerRolerService.deleteRoler(readerRoler);
			addMessage(redirectAttributes, "删除用户角色成功");
		}
		return "redirect:" + adminPath + "/book/roler/list?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required = false) String rdRoleId){
		List<Map<String, Object>> mapList = Lists.newArrayList();
		ReaderRoler roler = new ReaderRoler();
		roler.setRdRoleId(rdRoleId);
		List<ReaderRoler> list = readerRolerService.findList(roler);
		for(ReaderRoler e : list){
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getRdRoleId());
			map.put("name", e.getRoleName());
			mapList.add(map);
		}
		return mapList;
	}
}
